package com.yaodian100.ec.order.web;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.ec.order.entity.OrderPrize;
import com.yaodian100.ec.order.service.OrderPrizeService;

public class OrderPrizeAction extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5468961751265155690L;
	private final Logger logger = LoggerFactory.getLogger(OrderPrizeAction.class);
	@Resource
	private OrderPrizeService orderPrizeService;
	private SimplePager pager;
	// 集购 抽獎
	private List<OrderPrize> orderPrizes;
	private String prizeIdParam;
	private String tuangouIdParam;
	private String tuangouStParam;
	private String prizeMobileParam;
	private String prodNameParam;
	private String productIdParam;
	private InputStream ordserPrizeInputStream;
	private List<Object> orderPrizeList;
	private List<Object> orderPrizeExportList;
	private Map<String, Object> orderPrizeMap;
	private String loginIdParam;

	@Override
	public void prepare() throws Exception {
		clearErrorsAndMessages();

	}

	public String prizeSearch() throws Exception {
		return "prizeSearch";
	}

	/**
	 * 抽奖活动查询列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String queryPrize() throws Exception {
		orderPrizeMap = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(tuangouStParam)) {
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			orderPrizeMap.put("prizedate", tuangouStParam);
		}
		if (StringUtils.isNotBlank(prizeIdParam)) {
			orderPrizeMap.put("prizeId", Long.parseLong(prizeIdParam));
		}
		if (StringUtils.isNotBlank(tuangouIdParam)) {
			orderPrizeMap.put("tuangouId", Long.parseLong(tuangouIdParam));
		}
		if (StringUtils.isNotBlank(prizeMobileParam)) {
			orderPrizeMap.put("prizeMobile", prizeMobileParam);
		}
		if (StringUtils.isNotBlank(prodNameParam)) {
			orderPrizeMap.put("prodName", prodNameParam);
		}
		if (StringUtils.isNotBlank(loginIdParam)) {
			orderPrizeMap.put("loginId", loginIdParam.trim());
		}
		if (pager == null) {
			pager = new SimplePager();
		}
		int entitySize = orderPrizeService.getOrderPrizeSize(orderPrizeMap).intValue();
		pager.setTotalSize(entitySize);
		orderPrizeList = orderPrizeService.getOrderPrize(orderPrizeMap);
		return "prizeSearch";
	}

	public String prizeExport() throws Exception {
		return "prizeExport";
	}

	/**
	 * 抽奖活动汇出
	 * 
	 * @return
	 * @throws Exception
	 */
	public String exportPrize() throws Exception {
		orderPrizeMap = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(productIdParam)) {
			orderPrizeMap.put("productId", productIdParam);
		}
		if (StringUtils.isNotBlank(tuangouIdParam)) {
			orderPrizeMap.put("tuangouId", Long.parseLong(tuangouIdParam));
		}
		if (StringUtils.isNotBlank(tuangouStParam)) {
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			orderPrizeMap.put("prizedate", tuangouStParam);
		}
		if (StringUtils.isNotBlank(prodNameParam)) {
			orderPrizeMap.put("prodName", prodNameParam);
		}
		if (pager == null) {
			pager = new SimplePager();
		}
		int entitySize = orderPrizeService.getOrderPrizeExportSize(orderPrizeMap).intValue();
		pager.setTotalSize(entitySize);
		orderPrizeExportList = orderPrizeService.getOrderExportPrize(orderPrizeMap);
		return "prizeExport";
	}

	public String exportPrizeList() throws Exception {
		orderPrizeMap = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(tuangouIdParam)) {
			orderPrizeMap.put("tuangouId", Long.parseLong(tuangouIdParam));
		}
		orderPrizeExportList = orderPrizeService.getOrderPrize(orderPrizeMap);
		if (orderPrizeExportList.size() > 0) {

			Workbook workbook = exportExcel(orderPrizeExportList);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			workbook.write(out);
			ordserPrizeInputStream = new ByteArrayInputStream(out.toByteArray());
			out.flush();
			out.close();
			return "exportPrizeList";
		} else {
			this.addActionError("无资料可汇出");
			return prizeExport();
		}

	}

	public List<OrderPrize> getOrderPrizes() {
		return orderPrizes;
	}

	public void setOrderPrizes(List<OrderPrize> orderPrizes) {
		this.orderPrizes = orderPrizes;
	}

	public String getPrizeIdParam() {
		return prizeIdParam;
	}

	public void setPrizeIdParam(String prizeIdParam) {
		this.prizeIdParam = prizeIdParam;
	}

	public String getTuangouIdParam() {
		return tuangouIdParam;
	}

	public void setTuangouIdParam(String tuangouIdParam) {
		this.tuangouIdParam = tuangouIdParam;
	}

	public String getTuangouStParam() {
		return tuangouStParam;
	}

	public void setTuangouStParam(String tuangouStParam) {
		this.tuangouStParam = tuangouStParam;
	}

	public String getPrizeMobileParam() {
		return prizeMobileParam;
	}

	public void setPrizeMobileParam(String prizeMobileParam) {
		this.prizeMobileParam = prizeMobileParam;
	}

	public String getProdNameParam() {
		return prodNameParam;
	}

	public void setProdNameParam(String prodNameParam) {
		this.prodNameParam = prodNameParam;
	}

	public String getProductIdParam() {
		return productIdParam;
	}

	public void setProductIdParam(String productIdParam) {
		this.productIdParam = productIdParam;
	}

	public InputStream getOrdserPrizeInputStream() {
		return ordserPrizeInputStream;
	}

	public void setOrdserPrizeInputStream(InputStream ordserPrizeInputStream) {
		this.ordserPrizeInputStream = ordserPrizeInputStream;
	}

	private Workbook exportExcel(List orderPrizeExportList) throws Exception {
		Workbook wb = new HSSFWorkbook();
		CreationHelper ch = wb.getCreationHelper();
		Sheet sheet = wb.createSheet("抽奖汇出");
		sheet.createFreezePane(0, 1);
		sheet.setColumnWidth(0, 5000);
		sheet.setColumnWidth(1, 4000);
		sheet.setColumnWidth(2, 3000);
		sheet.setColumnWidth(3, 5000);
		sheet.setColumnWidth(4, 9000);
		sheet.setColumnWidth(5, 4000);

		short border = 1;
		CellStyle headerCellStyle = wb.createCellStyle();
		CellStyle bodyCellStyle = wb.createCellStyle();
		CellStyle bodyCellStyleDate = wb.createCellStyle();
		Font boldFont = wb.createFont();
		boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		headerCellStyle.setFont(boldFont);
		headerCellStyle.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
		headerCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		headerCellStyle.setBorderBottom(border);
		headerCellStyle.setBorderLeft(border);
		headerCellStyle.setBorderRight(border);
		headerCellStyle.setBorderTop(border);
		headerCellStyle.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
		headerCellStyle.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
		headerCellStyle.setRightBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
		headerCellStyle.setTopBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
		headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
		headerCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		bodyCellStyle.setBorderBottom(border);
		bodyCellStyle.setBorderLeft(border);
		bodyCellStyle.setBorderRight(border);
		bodyCellStyle.setBorderTop(border);
		bodyCellStyle.setAlignment(CellStyle.ALIGN_LEFT);
		bodyCellStyle.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
		bodyCellStyle.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
		bodyCellStyle.setRightBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
		bodyCellStyle.setTopBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
		bodyCellStyleDate.setBorderBottom(border);
		bodyCellStyleDate.setBorderLeft(border);
		bodyCellStyleDate.setBorderRight(border);
		bodyCellStyleDate.setBorderTop(border);
		bodyCellStyleDate.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
		bodyCellStyleDate.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
		bodyCellStyleDate.setRightBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
		bodyCellStyleDate.setTopBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
		bodyCellStyleDate.setAlignment(CellStyle.ALIGN_LEFT);
		bodyCellStyleDate.setDataFormat(ch.createDataFormat().getFormat("yyyy-mm-dd"));

		Row row = sheet.createRow(0);
		Cell cell = row.createCell(0);
		cell.setCellStyle(headerCellStyle);

		cell.setCellValue(ch.createRichTextString("抽奖编号"));
		cell = row.createCell(1);
		cell.setCellStyle(headerCellStyle);
		cell.setCellValue(ch.createRichTextString("会员名称"));
		cell = row.createCell(2);
		cell.setCellStyle(headerCellStyle);
		cell.setCellValue(ch.createRichTextString("手机号码"));
		cell = row.createCell(3);
		cell.setCellStyle(headerCellStyle);
		cell.setCellValue(ch.createRichTextString("所在城市"));
		cell = row.createCell(4);
		cell.setCellStyle(headerCellStyle);
		cell.setCellValue(ch.createRichTextString("商品名称"));
		cell = row.createCell(5);
		cell.setCellStyle(headerCellStyle);
		cell.setCellValue(ch.createRichTextString("订购日期"));

		for (int index = 0; index < orderPrizeExportList.size(); index++) {
			Object[] tmpAry = (Object[]) orderPrizeExportList.get(index);

			row = sheet.createRow(index + 1);
			cell = row.createCell(0);
			cell.setCellStyle(bodyCellStyle);
			cell.setCellValue(ch.createRichTextString(tmpAry[0].toString()));

			cell = row.createCell(1);
			cell.setCellStyle(bodyCellStyle);
			if (tmpAry[7].toString().length() > 3) {
				cell.setCellValue(ch.createRichTextString(StringUtils.overlay(StringUtils.defaultString(tmpAry[7].toString()),
						"***", 4, tmpAry[7].toString().length())));
			} else {
				cell.setCellValue(ch.createRichTextString(StringUtils.overlay(StringUtils.defaultString(tmpAry[7].toString()),
						"***", 2, tmpAry[7].toString().length())));
			}

			cell = row.createCell(2);
			cell.setCellStyle(bodyCellStyle);
			String prizeMobile = StringUtils.overlay(StringUtils.defaultString(tmpAry[8].toString()), "****", tmpAry[8]
					.toString().length() - 4, tmpAry[8].toString().length());
			cell.setCellValue(ch.createRichTextString(prizeMobile));

			cell = row.createCell(3);
			cell.setCellStyle(bodyCellStyle);
			cell.setCellValue(ch.createRichTextString(StringUtils.defaultString(tmpAry[9].toString())));

			cell = row.createCell(4);
			cell.setCellStyle(bodyCellStyle);
			cell.setCellValue(ch.createRichTextString(StringUtils.defaultString(tmpAry[3].toString())));

			cell = row.createCell(5);
			cell.setCellStyle(bodyCellStyle);
			cell.setCellValue(ch.createRichTextString(tmpAry[4].toString()));
		}
		return wb;
	}

	public List<Object> getOrderPrizeList() {
		return orderPrizeList;
	}

	public void setOrderPrizeList(List<Object> orderPrizeList) {
		this.orderPrizeList = orderPrizeList;
	}

	public Map<String, Object> getOrderPrizeMap() {
		return orderPrizeMap;
	}

	public void setOrderPrizeMap(Map<String, Object> orderPrizeMap) {
		this.orderPrizeMap = orderPrizeMap;
	}

	public String getLoginIdParam() {
		return loginIdParam;
	}

	public void setLoginIdParam(String loginIdParam) {
		this.loginIdParam = loginIdParam;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public List<Object> getOrderPrizeExportList() {
		return orderPrizeExportList;
	}

	public void setOrderPrizeExportList(List<Object> orderPrizeExportList) {
		this.orderPrizeExportList = orderPrizeExportList;
	}

	public OrderPrizeService getOrderPrizeService() {
		return orderPrizeService;
	}

	public void setOrderPrizeService(OrderPrizeService orderPrizeService) {
		this.orderPrizeService = orderPrizeService;
	}

}
